package com.dycong.common.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * todo 等待--->StringUtil
 * Created by ${dycong} on 2016/12/14.
 */
public class RegexPatternTools {


    private static Log log = LogFactory.getLog(RegexPatternTools.class);


    /**
     * 判断对象数组是否为空
     *@param obj 对象数组
     * @return 为空返回true 反之返回false
     */
    public static boolean isEmpty(Object[] obj) {
        if (obj != null && obj.length > 0) {
            return false;
        }
        return true;
    }

    /**
     *
     * @param name
     * @return
     */
    public static String wordFormat(String name){
        if (name==null){
            return null;
        }
        Pattern pattern=Pattern.compile("[A-Z]");
        Matcher matcher=pattern.matcher(name);
        StringBuffer stringBuffer=new StringBuffer();
        while (matcher.find()){
            matcher.appendReplacement(stringBuffer,("_"+matcher.group()).toLowerCase());
        }
        return matcher.appendTail(stringBuffer).toString().toLowerCase();
    }

    /**
     * 判断对象数组是否为空
     * @param obj 对象数组
     * @return 为空返回false 反之返回true
     */
    public static boolean isNotEmpty(Object[] obj) {
        if (obj != null && obj.length > 0) {
            return true;
        }
        return false;
    }

    /**
     * @param list
     * @return
     * @功能 判断集合是否等于null
     * @时间 2011-1-27 上午09:39:20
     */
    public static boolean isEmpty(List list) {
        if (list != null && !list.isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * ※判断字符串中是否仅包含字母，数字，下划线
     *
     * @param inputString String实例对象
     * @return 是返回true 反之返回false
     */
    public static boolean isWord(String inputString) {
        if (inputString != null) {
            return inputString.matches("^\\w+$");
        } else {
            return false;
        }
    }

    /**
     * @return
     * @功能 替换字符串内的% '
     * @作者 马东东
     * @时间 2010-12-14 下午03:45:38
     */
    public static String replaceAll(String hql) {
        hql = hql.replaceAll("'", "");
        hql = hql.replaceAll("%", "");
        return hql;
    }

    /**
     * 判断该字符是否为字母
     *
     * @param inputString
     * @return 是返回true 反之返回false
     */
    public static boolean isLetter(String inputString) {
        if (inputString != null) {
            return inputString.matches("[a-zA-Z]+");
        } else {
            return false;
        }
    }

    /**
     * 判断字符串是否为数字
     *
     * @param inputString
     * @return 是返回true 反之返回false
     */
    public static boolean isNumber(String inputString) {
        if (inputString != null) {
            return inputString.matches("[0-9]+");
        } else {
            return false;
        }
    }

    /**
     * 检测email地址是否合法
     *
     * @param smail String实例对象
     * @return 是 返回true 不是返回 false
     */
    public static boolean isMail(String smail) {
        Pattern pattern = Pattern.compile(
                "[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+",
                Pattern.CASE_INSENSITIVE);
        return pattern.matcher(smail).matches();
    }

    /**
     * 检测是否为合法的电话号码
     *
     * @param num String实例对象
     * @return 是 返回true 不是返回 false
     */
    public static boolean isPhoneNumber(String num) {
        return Pattern.matches("(\\(\\d{3}\\)|\\d{3,4}-)?\\d{7,8}$", num);
    }

    /**
     * 检测是否为合法的手机号码
     *
     * @param num
     * @return 是 返回true 不是返回 false
     */
    public static boolean isMobileNumber(String num) {
        return Pattern.matches("0?(13|15|18)\\d{9}", num);
    }

    /**
     * 检测是否为合法的邮编
     *
     * @param num
     * @return
     */
    public static boolean isZipCodeNumber(String num) {
        return Pattern.matches("\\d{6}", num);
    }

    /**
     * 判断字符是不是汉字
     *
     * @param str
     * @return
     */
    public static boolean isCharacter(String str) {
        boolean temp = false;
        temp = str.matches("[\\u4e00-\\u9fa5]");
        return temp;
    }

    /**
     * @param str 例子以下均为合格日期
     *            <p>
     *            (<ol><li>20101211 11:11:11</li>
     *            <p>
     *            <li>2010-12-11 11:11:11</li>
     *            <p>
     *            <li>2010/12/11 11:11:11</li>
     *            <p>
     *            <li>20101211</li>
     *            <p>
     *            <li>2010/12/11</li>
     *            <p>
     *            <li>2010-12-11</li></ol>
     *            <p>
     *            )
     * @return
     * @功能 验证日期
     * @作者 马东东
     * @时间 2010-12-14 上午11:48:17
     */
    public static boolean isDate(String str) {
        String eL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
//		 String eL= "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";


        Pattern pattern = Pattern.compile(eL);
        return pattern.matcher(str).matches();
    }

    /**
     * 将字符串分割为数组
     *
     * @param str        要分隔的字符串实例
     * @param expression 分割符
     * @return
     */
    public static Serializable[] splite(String str, String expression) {

        if (str != null && str != "") {
            StringTokenizer a = new StringTokenizer(str, expression);
            int i = 0;
            Serializable[] strs = new Serializable[a.countTokens()];
            if (a.countTokens() > 0) {
                try {
                    while (a.hasMoreTokens()) {
                        strs[i] = a.nextToken();
                        i++;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    log.error("数组越界", e);
                    e.printStackTrace();
                }

            }
            return strs;
        } else {
            Serializable[] o = {};
            return o;
        }
    }

    /**
     * 将字符串分割为数组
     *
     * @param str        要分隔的字符串实例
     * @param expression 分割符
     * @return
     */
    public static String[] stringSplite(String str, String expression) {

        if (str != null && str != "") {
            StringTokenizer a = new StringTokenizer(str, expression);
            int i = 0;
            String[] strs = new String[a.countTokens()];
            if (a.countTokens() > 0) {
                try {
                    while (a.hasMoreTokens()) {
                        strs[i] = a.nextToken();
                        i++;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    log.error("数组越界", e);
                    e.printStackTrace();
                }

            }
            return strs;
        } else {
            String[] o = {};
            return o;
        }
    }

    /**
     * 将输入的Serializable[]转换为String
     *
     * @param array        序列化数组
     * @param insertString 数组元素与元素之间连接字符，例如：array[0]+insertString+array[1]+insertString+....
     * @return
     */
    public static String spliteToString(Serializable[] array,
                                        String insertString) {
        String strs = "";
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                if (i < array.length - 1) {
                    strs += array[i] + insertString;
                } else {
                    strs += array[i];
                }
            }
        }
        return strs;
    }

    /**
     * 获得长度为n的随机数
     *
     * @param n 随机数指定长度
     * @return
     */
    public static String random(int n) {
        String s = "";
        for (int i = 0; i < n; i++) {
            int a = (int) (Math.random() * 10);
            s += a;
        }
        return s;
    }

    /**
     * 获得长度为n的随机数
     *
     * @param n 随机数指定长度
     * @return
     */
    public static String randomChar(int n) {

        String str = "";

        char[] cha = new char[52];
        int i = 0;
        for (; i < 26; i++) {
            cha[i] = (char) (65 + i);
        }
        for (; i < 52; i++) {
            cha[i] = (char) (71 + i);
        }
        // cha数组中，此时元素为A_Za-z


        Random random = new Random();
        for (i = 0; i < n; i++) {
            int rand = random.nextInt(52);
            str = str + cha[rand];
        }
        return str;
    }

    /**
     * 获得取随字母加数字 长度为n
     *
     * @param n 指定长度
     * @return
     */
    public static String randomString(int n) {

        String str = "";

        char[] cha = new char[62];
        int i = 0;
        for (; i < 26; i++) {
            cha[i] = (char) (65 + i);
        }
        for (; i < 52; i++) {
            cha[i] = (char) (71 + i);
        }
        for (; i < 62; i++) {
            cha[i] = (char) (-4 + i);
        }
        // cha数组中，此时元素为A_Za-z0-9


        Random random = new Random();
        for (i = 0; i < n; i++) {
            int rand = random.nextInt(62);
            str = str + cha[rand];
        }
        return str;
    }


    /**
     * 将String转换成unicode编码格式
     *
     * @param str String实例对象
     * @return
     */
    public static String unicodeEncoding(String str) {
        StringBuffer unicodeBytes = new StringBuffer();
        for (int byteIndex = 0; byteIndex < str.length(); byteIndex++) {
            String hexB = Integer.toHexString(str.charAt(byteIndex));
            unicodeBytes.append("\\u");
            if (hexB.length() <= 2) {
                unicodeBytes.append("00");
            }
            unicodeBytes.append(hexB);
        }

        return unicodeBytes.toString();
    }

    /**
     * Iso-8859-1转码
     */
    public static String encodeString(String beforeStr, String code) {
        try {
            if (beforeStr != null) {
                return new String(beforeStr.getBytes("ISO-8859-1"), code);
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("编码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * Iso-8859-1转码 默认为utf-8
     */
    public static String toUtf8(String beforeStr) {
        try {
            if (beforeStr != null) {
                return new String(beforeStr.getBytes("ISO-8859-1"), "utf-8");
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("编码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * urlencode转码
     */
    public static String encodeURI(String beforeStr, String code) {
        try {
            if (beforeStr != null) {
                return java.net.URLEncoder.encode(beforeStr, code);
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("编码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * urlencode转码 默认为utf-8
     */
    public static String encodeURI(String beforeStr) {
        try {
            if (beforeStr != null) {
                return java.net.URLEncoder.encode(beforeStr, "utf-8");
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("编码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 对参数进行url反编码
     */
    public static String dencodeURI(String beforeStr) {
        try {
            if (beforeStr != null) {
                return java.net.URLDecoder.decode(beforeStr, "utf-8");
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("解码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 对参数进行url反编码
     */
    public static String dencodeURI(String beforeStr, String code) {
        try {
            if (beforeStr != null) {
                return java.net.URLDecoder.decode(beforeStr, code);
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("解码错误", e);
            e.printStackTrace();
            return "";
        }

    }

    /**
     * sql 语句过滤
     *
     * @param sql
     * @return
     */
    public static String sqlCharFiter(String sql) {
        return sql
                .replaceAll(
                        "[\\[\\]'~!@#$%\\^&\\*\\(\\)_\\+\\|\\-=;,\\./\\{\\}:<>\\?`\\\\\"]+",
                        "");
    }

    public static void main(String[] args) {
        // System.out.println(isLetter("DDDD33"));


//		System.out.println(isNumber("55a55"));


    }

}
